//
//  HHShareActionSheet.m
//  HHBroker
//
//  Created by He Wei on 9/24/15.
//  Copyright © 2015 harry. All rights reserved.
//

#import "HHShareActionSheet.h"
#import "UIViewAdditions.h"
#import "HHShareButton.h"

#define SCREEN_SIZE [UIScreen mainScreen].bounds.size
#define JYColor(r, g, b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1.0f]
#define KTagOfButton 2000

@interface HHShareActionSheet ()
{
    /**  所有按钮 **/
    NSArray *_buttonTitles;
    /**  暗黑色的view **/
    UIView *_darkView;
    
    /**  所有按钮的低部view  **/
    UIView *_bottomView;
    
    id _deletate;

}

@property (nonatomic, strong) UIWindow *backWindow;

@end

@implementation HHShareActionSheet

+(instancetype)shareActionSheetWithDeletate:(id)deletate
{
    return [[self alloc] initWithDeletate:deletate];
}

- (instancetype)initWithDeletate:(id)deletate
{
    if (self = [super init]) {
        self.delegate = deletate;
        // 暗黑色的view
        UIView *darkView = [[UIView alloc] init];
        [darkView setAlpha:0];
        [darkView setUserInteractionEnabled:NO];
        [darkView setFrame:(CGRect){0, 0, SCREEN_SIZE}];
        [darkView setBackgroundColor:JYColor(46, 49, 50)];
        [self addSubview:darkView];
        _darkView = darkView;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss:)];
        [darkView addGestureRecognizer:tap];
        
        //所有按钮的底部view
        UIView *bottomView = [[UIView alloc] init];
        [bottomView setBackgroundColor:JYColor(214, 214, 222)];
        [self addSubview:bottomView];
        _bottomView = bottomView;
        
        CGFloat width = 60, height = 60+7+12;
        CGFloat whiteSpace = 44;
        CGFloat fixSpace = (SCREEN_SIZE.width-3*60-2*whiteSpace)/2;
        CGFloat fixHeight = 22;
        NSArray *titleArray = @[@"微信好友", @"朋友圈", @"短信", @"QQ", @"QQ空间"];
        NSArray *tagsArray = @[@(JYShareTypeWeiChat), @(JYShareTypeFriends), @(JYShareTypeMessage), @(JYShareTypeQzone), @(JYShareTypeQQ)];
        NSArray *imagesArray = @[@"img_share_wechat", @"img_share_friends", @"img_share_message", @"img_share_qq", @"img_share_qzone"];
        for (int i=0; i<titleArray.count; i++) {
            HHShareButton *button = [[HHShareButton alloc] init];
            button.frame = CGRectMake(whiteSpace+i%3*(fixSpace+width), 20+i/3*(fixHeight+height), width, height);
            button.tag = KTagOfButton+[tagsArray[i] integerValue];
            [button setImage:[UIImage imageNamed:imagesArray[i]] forState:UIControlStateNormal];
            [button setTitle:titleArray[i] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
            [bottomView addSubview:button];
        }
        //cancel button
        UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [cancelButton setTitleColor:UIColorFromRGB(0x007aff) forState:UIControlStateNormal];
        cancelButton.frame = CGRectMake(0, 20+(height+22)*2+13, SCREEN_SIZE.width, 24);
        [bottomView addSubview:cancelButton];
        cancelButton.titleLabel.font = [UIFont systemFontOfSize:24];
        [cancelButton addTarget:self action:@selector(didClickCancelBtn) forControlEvents:UIControlEventTouchUpInside];
        
        CGFloat bottomH = 20+(height+fixHeight)*2 + 13 + 24 + 15;
        [bottomView setFrame:CGRectMake(0, SCREEN_SIZE.height, SCREEN_SIZE.width, bottomH)];
        bottomView.frame = CGRectMake(0, SCREEN_SIZE.height, SCREEN_SIZE.width, bottomH);
        bottomView.backgroundColor = [UIColor whiteColor];
        [self setFrame:(CGRect){0, 0, SCREEN_SIZE}];
        [self.backWindow addSubview:self];
    }

    return self;

}

- (void)didClickBtn:(UIButton *)btn
{
    [self dismiss:nil];

}

- (UIWindow *)backWindow
{
    if (_backWindow == nil) {
        _backWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _backWindow.windowLevel = UIWindowLevelStatusBar;
        _backWindow.backgroundColor = [UIColor clearColor];
        _backWindow.hidden = NO;
    }
    return _backWindow;
}

- (void)dismiss:(UITapGestureRecognizer *)tap
{
    [UIView animateWithDuration:0.3f
                     delay:0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         [_darkView setAlpha:0];
                         [_darkView setUserInteractionEnabled:NO];
                         
                         CGRect frame = _bottomView.frame;
                         frame.origin.y += frame.size.height;
                         [_bottomView setFrame:frame];
                     } completion:^(BOOL finished) {
                         [self removeFromSuperview];
                     }];
}

- (void)didClickCancelBtn
{
    [UIView animateWithDuration:0.3f
                          delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                              [_darkView setAlpha:0];
                              [_darkView setUserInteractionEnabled:NO];
                              
                              CGRect frame = _bottomView.frame;
                              frame.origin.y += frame.size.height;
                              [_bottomView setFrame:frame];
                          
                          
                          } completion:^(BOOL finished) {
                              [self removeFromSuperview];
                          
                          }];

}

- (void)show
{
    [UIView animateWithDuration:0.3f
delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
    [_darkView setAlpha:0.4f];
    [_darkView setUserInteractionEnabled:YES];
    
    CGRect frame = _bottomView.frame;
    frame.origin.y -= frame.size.height;
    [_bottomView setFrame:frame];

} completion:nil];

}

- (void)buttonClicked:(UIButton *)button
{
    NSInteger tag = button.tag - KTagOfButton;
    if (self.delegate && [self.delegate respondsToSelector:@selector(shareTo:)]) {
        [self.delegate  shareTo:tag];
        [self dismiss:nil];
    }
}
@end
